/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.bo;

import java.util.Date;

import com.mr.qa.dao.impl.MessageDAO;

/**
 * when an user sends a message, there are two records in database, one is
 * from(sent) message, another is to message. from messages can be listed in
 * sent box. to messges can be listed in in box
 */

public class Message extends BaseBO {

	// Fields

	private User mrIaskUserByToUserId;
	private User mrIaskUserByFromUserId;

	private String fromUserLoginName;
	private String toUserLoginName;

	private String title;
	private String content;
	private Date postDate;
	private String state;
	// 2008.11.22
	private String type = "0";// '0' received, '1' sent

	/** default constructor */
	public Message() {
	}

	public void setMrIaskUserByToUserId(User mrIaskUserByToUserId) {
		this.mrIaskUserByToUserId = mrIaskUserByToUserId;
	}

	public User getMrIaskUserByToUserId() {
		return mrIaskUserByToUserId;
	}

	public void setMrIaskUserByFromUserId(User mrIaskUserByFromUserId) {
		this.mrIaskUserByFromUserId = mrIaskUserByFromUserId;
	}

	public User getMrIaskUserByFromUserId() {
		return mrIaskUserByFromUserId;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}

	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}

	public Date getPostDate() {
		return postDate;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getState() {
		return state;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}

	public void setFromUserLoginName(String fromUserLoginName) {
		this.fromUserLoginName = fromUserLoginName;
	}

	public String getFromUserLoginName() {
		return fromUserLoginName;
	}

	public void setToUserLoginName(String toUserLoginName) {
		this.toUserLoginName = toUserLoginName;
	}

	public String getToUserLoginName() {
		return toUserLoginName;
	}

	public void sendMessage(String title, String content, Date date,
			User from, User to, MessageDAO messageDAO) {
		insertFromMessage(title, content, date, from, from.getLoginName(), to,
				to.getLoginName(), messageDAO);
		insertToMessage(title, content, date, from, from.getLoginName(), to, to
				.getLoginName(), messageDAO);
	}

	private void insertFromMessage(String title, String content, Date date,
			User from, String fromUserLoginName, User to,
			String toUserLoginName, MessageDAO messageDAO) {
		Message message = new Message();
		message.setTitle(title);
		message.setContent(content);
		message.setPostDate(date);
		message.setMrIaskUserByFromUserId(from);
		message.setMrIaskUserByToUserId(to);
		message.setFromUserLoginName(fromUserLoginName);
		message.setToUserLoginName(toUserLoginName);
		message.setState("0");
		message.setType("1");
		messageDAO.save(message);
	}

	private void insertToMessage(String title, String content, Date date,
			User from, String fromUserLoginName, User to,
			String toUserLoginName, MessageDAO messageDAO) {
		Message toMessage = new Message();
		toMessage.setTitle(title);
		toMessage.setContent(content);
		toMessage.setPostDate(date);
		toMessage.setMrIaskUserByFromUserId(from);
		toMessage.setMrIaskUserByToUserId(to);
		toMessage.setFromUserLoginName(fromUserLoginName);
		toMessage.setToUserLoginName(toUserLoginName);
		toMessage.setState("0");
		toMessage.setType("0");
		messageDAO.save(toMessage);
	}
}
